package com.zhuss.car.processor;

import java.util.Date;

import com.zhuss.car.processor.cache.DriverCache;
import com.zhuss.exception.ExceptionCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import com.zhuss.bean.bo.car.driver.DeleteDriverBo;
import com.zhuss.bean.vo.car.driver.DetailDriverVo;
import com.zhuss.common.AbstractProcessor;
import com.zhuss.common.Processor;
import com.zhuss.car.db.dao.ext.DriverDao;
import com.zhuss.car.db.entity.Driver;
import com.zhuss.db.entity.OpLog;
import com.zhuss.enums.LogActionEnum;
import com.zhuss.enums.MoudleEnum;
import com.zhuss.exception.DzcxException;
import com.zhuss.util.JsonUtil;

/**
 * @description: 司机删除
 * @author: zhushanshuo
 * @create: Mon Dec 21 13:31:45 CST 2020
 **/
@Component("driverDelete")
public class DriverDelete extends AbstractProcessor<Object, DeleteDriverBo> implements Processor<Object, DeleteDriverBo> {

    @Autowired
    private DriverDao driverDao;

    @Autowired
	@Qualifier("driverRead")
	private DriverRead driverRead;

	@Autowired
	@Qualifier("driverCache")
	private DriverCache driverCache;

    /**
     * 校验
     */
	@Override
    public void check(DeleteDriverBo bo) throws DzcxException {
		//驾驶员已排班不能被删除  （根据时间，查询班次是否被占用）
		if (driverDao.trainByDriverId(bo.getDriverId()) > 0) {
			throw new DzcxException(ExceptionCode.该驾驶员已被排班次);
		}
		if (bo.getDriverId() == null) {
			throw new DzcxException(ExceptionCode.驾驶员ID为空);
		}
    }

    /**
     * 操作方法
     */
    @Override
    public Object process(DeleteDriverBo param) throws DzcxException {
    	Long driverId = param.getDriverId();
    	String name = ((DetailDriverVo) driverRead.detail(driverId)).getName();

    	Driver record = new Driver();
    	record.setDriverId(driverId);
    	record.setDeleted(param.getDeleted());

    	record.setModifierId(param.getConcurrentUserId());
    	record.setModifier(param.getConcurrentUserName());
    	record.setModifyTime(new Date());
    	driverDao.delete(record);
    	param.setName(name);
		driverCache.clear(driverId);
        return null;
    }

    /**
     * 操作日志
     */
	@Override
	public Object log(DeleteDriverBo param) throws DzcxException {
		OpLog record = new OpLog();
    	record.setMoudle(MoudleEnum.车辆.key());
    	record.setAction(LogActionEnum.司机删除.key());
    	record.setTargetId(param.getDriverId());
    	record.setTargetName(param.getName());
    	record.setContent("参数: " + JsonUtil.toJson(param));
    	record.setReason(param.getExceptionReason());

    	record.setCreatorId(param.getConcurrentUserId());
    	record.setCreator(param.getConcurrentUserName());
    	record.setCreateTime(new Date());
    	log(record);
    	return null;
	}
}
